class code1 {
    //按奇偶排序数组：遍历数组，对前后元素进行交换
    public int[] sortArrayByParity(int[] nums) {
        int n = nums.length;
        int left = 0;
        int right = n-1;
        while(left<right) {
            while(left<n && nums[left]%2==0) {
                left++;
            }
            while(right>=0 && nums[right]%2==1) {
                right--;
            }
            //需要进行判断，防止下标越界
            if(left<right) {
                int tmp = nums[left];
                nums[left++] = nums[right];
                nums[right--] = tmp;
            }

        }
        return nums;
    }
}